package com.jeeplus.modules.guide.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.guide.mapper.MobilePublicSecurityMapper;
import com.jeeplus.modules.guide.mapper.MobilePublicSecurityTwoMapper;
import com.jeeplus.modules.guide.model.sw.WinValue;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@DS("queuework")
@RequiredArgsConstructor
@Service
@Slf4j
public class MobilePublicSecurityTwoService {
    private final MobilePublicSecurityTwoMapper mobilePublicSecurityTwoMapper;

    /**
     * 修改取号时间为当前时间
     *jxy
     * @returnid
     */
    @Transactional(readOnly = false)
    public int setComeTime( String number){
        return mobilePublicSecurityTwoMapper.setComeTime(number);
    }

    public AjaxJson findWinList(){
        List <WinValue> winList = mobilePublicSecurityTwoMapper.findWinList();
        List<WinValue> winListA = new ArrayList <WinValue>();
        List<WinValue> winListB = new ArrayList<WinValue>();
        List<WinValue> winListC = new ArrayList<WinValue>();
        int totalA = 0;//办理中
        int totalB = 0;//空闲中
        int totalC = 0;//未登录
        Boolean flag;
        for(WinValue wv : winList){
            if(wv.getWinNo().length() == 1){
                wv.setWinName("0"+wv.getWinNo());
            }else{
                wv.setWinName(wv.getWinNo());
            }
            if("L".equals(wv.getStatus())){
//                if("0000".equals(wv.getUserNo())){
//                    totalC++;
//                    wv.setStatus("N");
//                    wv.setStatusName("未登录");
//                    wv.setDoNumber("");
//                    wv.setNumberNum("0");
//                }else{
                    flag = false;
                    if(StringUtils.isNotBlank(wv.getNumberNum())){
                        if(checkValue(winList,wv.getUserNo()) == 1){
                            flag = true;
                        }
                    }
                    if(flag == true && !wv.getIsReserve().equals("1")){
                        totalA++;
                        wv.setStatus("W");
                        wv.setStatusName("办理中");
                    }else{
                        if(wv.getIsReserve().equals("1")){
                            totalC++;
                            wv.setStatus("N");
                            wv.setStatusName("预留");
                            wv.setDoNumber("");
                            wv.setNumberNum("0");
                            wv.setUserName("预留窗口");
                        }else{
                            totalB++;
                        wv.setStatusName("空闲中");
                        wv.setDoNumber("");
                        wv.setUserName("请稍候");
                        }
                        if(StringUtils.isNotBlank(wv.getPhoto()))
                            wv.setPhoto(wv.getPhoto().replace(wv.getUserNo(),"kx"));
                        else wv.setPhoto("/api/userfiles/1/程序附件/employee/employee/zhaopian/kx.jpg");
//                        totalB++;
//                        wv.setStatusName("空闲中");
//                        wv.setDoNumber("");
//                        wv.setUserName("请稍候");
//                        if(StringUtils.isNotBlank(wv.getPhoto()))
//                            wv.setPhoto(wv.getPhoto().replace(wv.getUserNo(),"kx"));
//                        else wv.setPhoto("/api/userfiles/1/程序附件/employee/employee/zhaopian/kx.jpg");
//                    }
                }
            }else if("W".equals(wv.getStatus()) || "S".equals(wv.getStatus())){
                totalA++;
                wv.setStatus("W");
                wv.setStatusName("办理中");
            }
            if(StringUtils.isBlank(wv.getNumberNum())){
                wv.setNumberNum("0");
            }
            if("5101090000004".equals(wv.getAreaId())){
                wv.setWinName("A"+wv.getWinName());
                winListA.add(wv);
            }else if("5101090000005".equals(wv.getAreaId())){
                wv.setWinName("B"+wv.getWinName());
                winListB.add(wv);
            }else{
                wv.setWinName("C"+wv.getWinName());
                winListC.add(wv);
            }
        }
        winList.clear();
        return AjaxJson.success().put("dataA",winListA).put("dataB",winListB).put("dataC",winListC).put("totalA",totalA).put("totalB",totalB).put("totalC",totalC);
    }

    public Integer checkValue(List <WinValue> winList,String key){
        int count = 0;
        for(WinValue wv : winList){
            if(key.equals(wv.getUserNo())){
                count++;
                if(count >= 2)break;
            }
        }
        return count;
    }

    //修改业务主题
    @Transactional(readOnly = false)
    public int updateTypeTheme(String typeName,String typeId){
        return mobilePublicSecurityTwoMapper.updateTypeTheme(typeName,typeId);
    }
    //修改业务部门
    @Transactional(readOnly = false)
    public int updateTypeManager(String managerName,String typeId){
        return mobilePublicSecurityTwoMapper.updateTypeManager(managerName,typeId);
    }

    public HashMap <String,String> findUserValueByUUID(String idcard){
        return mobilePublicSecurityTwoMapper.findUserValueByUUID(idcard);
    }

}
